package com.zrrd.yt.webll.controller;

import com.zrrd.yt.cart.client.CartFeignClient;
import com.zrrd.yt.model.product.SkuInfo;
import com.zrrd.yt.product.client.ProductFeigClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class CartController {
    @Autowired
    private CartFeignClient cartFeignClient;
    @Autowired
    private ProductFeigClient productFeigClient;

    //添加购物车
    @RequestMapping("addCart.html")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("skuNum") Integer skuNum,
                          HttpServletRequest request){
        //通过feign请求购物车服务添加购物车
        cartFeignClient.addToCart(skuId,skuNum);
        //通过feign请求商品服务获取sku基本信息
        SkuInfo skuInfo = productFeigClient.getSkuInfo(skuId);
        //将信息存入到请求域对象中
        request.setAttribute("skuInfo",skuInfo);
        request.setAttribute("skuNum",skuNum);
        //将数据转发给页面
        return "cart/addCart";
    }

    //查询购物车
    @RequestMapping("cart.html")
    public String index(){
        return "cart/index";
    }
}
